package com.home.customtest

import com.custom.action.condition.Conditions
import com.custom.action.condition.DefaultConditionWrapper
import com.custom.action.condition.KtConditionWrapper
import com.custom.jdbc.session.DbTxLevel
import com.home.customtest.data.StudentInfo
import org.junit.Test
import com.home.customtest.AssertTest.*
import com.home.customtest.data.StudentOnInsertOrUpdate
import com.home.customtest.data.StudentOnQueryAfter
import com.home.customtest.data.UserInfo
import java.math.BigDecimal

/**
 * @author XiaoBai
 * @since 2024/3/10 0010 0:09
 *
 */
class ApiKtTest :BaseTestConfig() {




    @Test
    fun testActiveModel() {
        activeModelSelect(2, true)
        activeModelInsert()
        activeModelUpdate()
        activeModelDelete()
    }

    private fun activeModelDelete() {

        val userInfo = UserInfo().apply {
            this.age = 22
            this.classId = 2
            this.deptId = 4
            this.name = "PPPm"
            this.state = 0
        }

        try {
            jdbcKtDao.execTrans {
                userInfo.save()
                val selectUserInfo = activeModelSelect(userInfo.id!!, false)
                isTrue(selectUserInfo.id == userInfo.id)
                isTrue(selectUserInfo.age == userInfo.age)
                isTrue(selectUserInfo.name.equals(userInfo.name))

                userInfo.delete()
                val deleteUserInfo = activeModelSelect(userInfo.id!!, false)
                isTrue(deleteUserInfo.id == userInfo.id)
                isTrue(deleteUserInfo.age  == null)
                isTrue(deleteUserInfo.name  == null)

                var a = 1 / 0
            }
        }catch (_: ArithmeticException) {}

    }

    private fun activeModelUpdate() {
        try {
            jdbcKtDao.execTrans{
                val userInfo = activeModelSelect(2, false)
                userInfo.age = 25
                userInfo.update()

                val userInfo2 = activeModelSelect(2, false)
                isTrue(userInfo2.age == userInfo.age)

                var a = 1 / 0
            }
        }catch (_: ArithmeticException) {}


    }

    private fun activeModelInsert() : UserInfo {

        val userInfo = UserInfo().apply {
            this.age = 23
            this.classId = 2
            this.deptId = 4
            this.name = "Bgfn"
            this.state = 0
        }

        try {
            jdbcKtDao.execTrans(DbTxLevel.TX_RC) {
                userInfo.insert()
                val info = activeModelSelect(userInfo.id!!, false)
                isTrue(info.id != null)
                isTrue(info.age != null)
                isTrue(info.name != null)
                isTrue(info.deptId != null)
                isTrue(info.classId != null)

                var a = 1 / 0
            }
        }catch (_: ArithmeticException) {}
        return userInfo
    }

    private fun activeModelSelect(id: Int, check: Boolean) :UserInfo {
        val userInfo = UserInfo().apply {
            this.id = id
            this.selectKeySet()
        }
        if (!check) {
            return userInfo
        }
        isTrue(userInfo.id != null)
        isTrue(userInfo.age != null)
        isTrue(userInfo.name != null)
        isTrue(userInfo.userClassName != null)
        isTrue(userInfo.createUserId != null)
        isTrue(userInfo.deptNameStr != null)
        return userInfo
    }


    @Test
    fun testDbJoinTableInherit() {
        val userInfo = jdbcKtDao.selectOne(Conditions.ktQuery(UserInfo::class.java)
                .eq(UserInfo::id, 2)
        )
        isTrue(userInfo.userClassName != null)
        isTrue(userInfo.createUserId != null)
        isTrue(userInfo.deptNameStr != null)
    }


    @Test
    fun testKtConditionWrapper() {
        val wrapper = KtConditionWrapper(StudentInfo::class.java)
        wrapper.eq(StudentInfo::name, "林涂")
        val studentInfo = jdbcKtDao.selectOne(wrapper)
        notNull(studentInfo)
    }

    @Test
    fun testCondition() {
        val wrapper = DefaultConditionWrapper(StudentInfo::class.java)
                .eq("a.age", 18)
                .toKtWrapper()
                .eq(StudentInfo::cityId, 1)
                .or().eq(StudentInfo::name, "zhangsan")
                .and(false) { it.eq(StudentInfo::address, "abc") }
                .or(true) { it.eq(StudentInfo::money, BigDecimal("2544.64")).or().between(StudentInfo::age, 18, 20) }
                .and(false) { it.le(StudentInfo::sex, false) }
                .notLike(StudentInfo::address, "长沙")
        val conditional = wrapper.finalConditional
        println(conditional)
        val paramValues = wrapper.paramValues
        isTrue(paramValues[0].equals(18))
        isTrue(paramValues[1].equals(1))
        isTrue(paramValues[2].equals("zhangsan"))
        isTrue(paramValues[3].equals(BigDecimal("2544.64")))
        isTrue(paramValues[4].equals(18))
        isTrue(paramValues[5].equals(20))
        isTrue(paramValues[6].equals("长沙"))
    }

    @Test
    fun testEntityInsertOrUpdateBefore() {

        try {
            jdbcKtDao.execTrans(DbTxLevel.TX_RC) {
                val insert = StudentOnInsertOrUpdate().apply {
                    this.name = "AbCOP"
                    this.nickName = "Nick——COP"
                    this.password = "122456"
                }
                insert.save()
                val select = jdbcKtDao.selectOne(Conditions.ktQuery(StudentOnInsertOrUpdate::class.java)
                        .eq(StudentOnInsertOrUpdate::id, insert.id)
                )
                isTrue(select.phone == "1555429610")

                val update = StudentOnInsertOrUpdate().apply {
                    this.id = insert.id
                    this.name = "AbCOP"
                    this.nickName = "Nick——COP"
                    this.password = "122456"
                }
                update.update()
                val select2 = jdbcKtDao.selectOne(Conditions.ktQuery(StudentOnInsertOrUpdate::class.java)
                        .eq(StudentOnInsertOrUpdate::id, insert.id)
                )
                isTrue(select2.phone == "1555429612")


                val queryAfter = jdbcKtDao.selectOne(Conditions.ktQuery(StudentOnQueryAfter::class.java)
                        .eq(StudentOnQueryAfter::id, insert.id)
                )
                isTrue(queryAfter.phone == "手机号：1555429612")

                var a = 1 / 0
            }
        }catch (_: ArithmeticException) {}


    }


}